/**
 * 文档作者: wj
 * 创建时间：2018年05月20日
 * 修改时间：2018年05月20日
 * 描述信息：收获地址界面
 */
import React, {Component} from 'react';
import './address.scss';
import util from '../../util/util';
import userModel from '../api/user.model';
import windowService from '../api/window.service';
import Alert from '../components/alert/alert';

class Address extends Component {
    constructor(props) {
        super(props);
        this.state = {
            addressList: undefined,
            isChange: false,
            isAlert: false
        }
        this.saveNode = undefined;
    }

    componentDidMount() {
        util.setTitle('收货信息');
        util.getApiData('post', '/svcShop/svcOpsAddress', {
            uid: userModel.svcRegWithWx.uid,
            apptoken: userModel.svcRegWithWx.apptoken,
            ops_type: "query",
        }).then(res => {
            this.setState({
                addressList: res.data.info.data
            })
        })
    }


    render() {
        return this.state.addressList ?
        <div className='address-page top-center'>
            {
                this.state.addressList.map(value => {
                    if (value.select === undefined) value.select = false;
                    return <div className='shadow' key={value.id}>
                        <p>{value.province}&nbsp;{value.city}</p>
                        <p className='gray'>{value.address}
                            <img onClick={this.onRadio.bind(this, value)} className='select' src={require(`../../assets/img/${value.select ? 'select.png' : 'radio.png'}`)} alt='logo'/>
                        </p>
                        <p className='gray'>联系人:{value.contact},{value.contact_cellno}</p>
                    </div>
                })
            }
            <Alert isShow={this.state.isAlert}
                info='确定删除?'
                onDefine={this.onDefine.bind(this)}
                onCancel={this.onCancel.bind(this)}></Alert>
            <button onClick={this.onAddress.bind(this, 'add')} className='btn grean'>+</button>
            <button onClick={this.onAddress.bind(this, 'update')} className='btn grean'>修改</button>
            {util.getQueryStringByName('sku') && <button onClick={this.onSubmit.bind(this)} className='btn grean'>确定</button>}
            <button onClick={this.onDelete.bind(this)} className='btn delete'>删除</button>
        </div> : '';
    }

    /**
     * 添加/修改地址
     * @param {add | update} 添加还是更新
     */
    onAddress(param) {
        if (this.saveNode === undefined && param === 'update') {
            window.$message.next({info: 'error', content: '请选择收获地址'});
            return;
        } 
        this.props.history.push(`/add?${param}${window.location.search ? `&${window.location.search.replace('?', '')}` : ''}`);
    }

    onSubmit() {
        if (this.saveNode !== undefined) {
            this.props.history.push(`/details${window.location.search}`);
        }
        // this.props.history.push(`/add?${param}`);
    }

    /**
     * 选中地址
     * @param {Object} 节点属性
     */
    onRadio(node) {
        if (this.saveNode !== undefined) this.saveNode.select = false;
        node.select = !node.select;
        this.saveNode = node;
        windowService.setAddressNode(node);
        this.setState({
            isChange: !this.state.isChange
        })
    }

    /**
     * 删除节点
     * @param {Object} node 节点属性
     */
    onDelete(node) {
        if (this.saveNode) {
            this.setState({
                isAlert: true
            })
        }
    }

    /** 点击取消 */
    onCancel() {
        this.setState({
            isAlert: false
        })
    }

    /** 点击确定 */
    onDefine() {
        if (this.saveNode !== undefined) {
            util.getApiData('post', '/svcShop/svcOpsAddress', {
                uid: userModel.svcRegWithWx.uid,
                apptoken: userModel.svcRegWithWx.apptoken,
                ops_type: "delete",
                udata: this.saveNode
            }).then(res => {
                if (res.data.res === 1) {
                    this.setState({
                        addressList: util.removeArray(this.state.addressList, this.saveNode.id),
                        isAlert: false
                    })
                    this.saveNode = undefined;
                    windowService.setAddressNode(undefined);
                }
            })
        }
    }
}

export default Address;
